package com.lzm.listendemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_socket);
        try {
            initSocket();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void initSocket() throws IOException {
        ServerSocket ss = new ServerSocket(8000);
        while (true) {
            Socket s = ss.accept();//接受一个连接
            DataInputStream dis = new DataInputStream(s.getInputStream());//输入管道
            System.out.println(dis.readUTF());
            dis.close();
            s.close();
        }
    }
}